package com.sg.service.base.component.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.dto.FieldInfo;
import com.sg.common.exception.BizException;
import com.sg.common.tools.StringUtils;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.component.req.BatchQueryVirtualImageListByCodesReq;
import com.sg.dto.base.component.req.BatchQueryVirtualImageListByIdsReq;
import com.sg.dto.base.component.req.QueryVirtualImageDetailReq;
import com.sg.dto.base.component.req.QueryVirtualImageListReq;
import com.sg.entity.OmsMessageVirtualImage;
import com.sg.mapper.OmsMessageVirtualImageMapper;
import com.sg.service.base.component.MOmsMessageVirtualImageService;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsMessageVirtualImageServiceImpl
        extends ServiceImpl<OmsMessageVirtualImageMapper, OmsMessageVirtualImage>
        implements MOmsMessageVirtualImageService {

    @Autowired
    OmsMessageVirtualImageMapper omsMessageVirtualImageMapper;

    /**
     * 2-5-06新增虚拟形象[6730]
     * gen by moon at 5/16/2023, 8:51:20 PM
     */
    @Trace(operationName = "2-5-06新增虚拟形象")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addVirtualImage(OmsMessageVirtualImage omsMessageVirtualImage) {
        // TODO: 2024/1/20
        if (!StringUtils.isEmpty(omsMessageVirtualImage.getVirtualImage())) {
            omsMessageVirtualImage.setVirtualImage(CommonFunctionHelper.replaceFilePath(omsMessageVirtualImage.getVirtualImage()));
        }
        omsMessageVirtualImage.setVirtualImageId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsMessageVirtualImage.setIsArchive("FALSE");
        omsMessageVirtualImage.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsMessageVirtualImage.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsMessageVirtualImage != null && omsMessageVirtualImage.getSubjectLifeCycle() != null && omsMessageVirtualImage.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsMessageVirtualImage.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsMessageVirtualImage.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsMessageVirtualImage.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsMessageVirtualImage != null && omsMessageVirtualImage.getSubjectLifeCycle() != null && omsMessageVirtualImage.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsMessageVirtualImage.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsMessageVirtualImage.setIsValid("FALSE");
        omsMessageVirtualImage.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsMessageVirtualImage.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsMessageVirtualImage)) {
            return omsMessageVirtualImage.getVirtualImageId();
        } else {
            return "-1";
        }

    }

    /**
     * 2-5-06修改虚拟形象[6731]
     * gen by moon at 5/16/2023, 8:51:21 PM
     */
    @Trace(operationName = "2-5-06修改虚拟形象")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateVirtualImage(OmsMessageVirtualImage omsMessageVirtualImage) {
        // TODO: 2024/1/20
        if (!StringUtils.isEmpty(omsMessageVirtualImage.getVirtualImage())) {
            omsMessageVirtualImage.setVirtualImage(CommonFunctionHelper.replaceFilePath(omsMessageVirtualImage.getVirtualImage()));
        }
        omsMessageVirtualImage.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsMessageVirtualImage != null && omsMessageVirtualImage.getSubjectLifeCycle() != null && omsMessageVirtualImage.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsMessageVirtualImage.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsMessageVirtualImage != null && omsMessageVirtualImage.getSubjectLifeCycle() != null && omsMessageVirtualImage.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsMessageVirtualImage.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsMessageVirtualImage.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsMessageVirtualImage != null && omsMessageVirtualImage.getSubjectLifeCycle() != null && omsMessageVirtualImage.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsMessageVirtualImage.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsMessageVirtualImage != null && omsMessageVirtualImage.getSubjectLifeCycle() != null && omsMessageVirtualImage.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsMessageVirtualImage.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(omsMessageVirtualImage);
    }

    /**
     * 2-5-06删除虚拟形象[6732]
     * gen by moon at 5/16/2023, 8:51:21 PM
     */
    @Trace(operationName = "2-5-06删除虚拟形象")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteVirtualImage(String virtualImageId) {

        return super.removeById(virtualImageId);
    }

    /**
     * 2-5-06查询虚拟形象详情[6733]
     * gen by moon at 5/16/2023, 8:51:24 PM
     */
    @Trace(operationName = "2-5-06查询虚拟形象详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsMessageVirtualImage queryVirtualImageDetail(QueryVirtualImageDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsMessageVirtualImage> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsMessageVirtualImage> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsMessageVirtualImage retData = retList.get(0);
                // TODO: 2024/5/30
                if (retData.getVirtualImage() != null) {
                    retData.setVirtualImage(CommonFunctionHelper.getFilePath(retData.getVirtualImage()));
                }
                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-5-06查询虚拟形象列表[6734]
     * gen by moon at 6/20/2023, 6:09:05 PM
     */
    @Trace(operationName = "2-5-06查询虚拟形象列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsMessageVirtualImage> queryVirtualImageList(QueryVirtualImageListReq reqDto) {

        try {
            QueryWrapper<OmsMessageVirtualImage> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsMessageVirtualImage.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsMessageVirtualImage.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsMessageVirtualImage.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            // TODO: 2024/5/30
            List<OmsMessageVirtualImage> retList = super.list(queryWrapper);
            for (OmsMessageVirtualImage one : retList) {
                one.setVirtualImage(CommonFunctionHelper.getFilePath(one.getVirtualImage()));
            }
            return retList;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-5-06批量查询虚拟形象列表ByIds[6901]
     * gen by moon at 5/21/2023, 9:55:38 PM
     */
    @Trace(operationName = "2-5-06批量查询虚拟形象列表ByIds")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsMessageVirtualImage> batchQueryVirtualImageListByIds(BatchQueryVirtualImageListByIdsReq reqDto) {

        try {
            QueryWrapper<OmsMessageVirtualImage> queryWrapper = new QueryWrapper<>();

            if (reqDto.getVirtualImageList() == null || reqDto.getVirtualImageList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("virtual_image_id", reqDto.getVirtualImageList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsMessageVirtualImage.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsMessageVirtualImage.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsMessageVirtualImage.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            // TODO: 2024/5/30  
            List<OmsMessageVirtualImage> retList = super.list(queryWrapper);
            for (OmsMessageVirtualImage one : retList) {
                one.setVirtualImage(CommonFunctionHelper.getFilePath(one.getVirtualImage()));
            }
            return retList;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-5-06查询虚拟形象列表ByCodes[7611]
     * gen by moon at 10/26/2023, 11:00:00 PM
     */
    @Trace(operationName = "2-5-06查询虚拟形象列表ByCodes")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsMessageVirtualImage> batchQueryVirtualImageListByCodes(BatchQueryVirtualImageListByCodesReq reqDto) {

        try {
            QueryWrapper<OmsMessageVirtualImage> queryWrapper = new QueryWrapper<>();

            if (reqDto.getVirtualImageList() == null || reqDto.getVirtualImageList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("virtual_image_code", reqDto.getVirtualImageList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsMessageVirtualImage.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsMessageVirtualImage.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsMessageVirtualImage.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            // TODO: 2024/5/30  
            List<OmsMessageVirtualImage> retList = super.list(queryWrapper);
            for (OmsMessageVirtualImage one : retList) {
                one.setVirtualImage(CommonFunctionHelper.getFilePath(one.getVirtualImage()));
            }
            return retList;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
